package cn.bxshey.resourceSharing.controller;

import cn.bxshey.resourceSharing.dto.essay.EssayDto;
import cn.bxshey.resourceSharing.dto.essay.SupportDto;
import cn.bxshey.resourceSharing.entity.Essay;
import cn.bxshey.resourceSharing.entity.R;
import cn.bxshey.resourceSharing.entity.User;
import cn.bxshey.resourceSharing.service.EssayService;
import cn.bxshey.resourceSharing.utils.JwtUtils;
import cn.bxshey.resourceSharing.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * (Essay)表控制层
 *
 * @author makejava
 * @since 2023-12-16 17:54:45
 */
@RestController
@RequestMapping("/essay")
public class EssayController {
    /**
     * 服务对象
     */
    @Resource
    private EssayService essayService;

    @Resource
    private RedisUtils redisUtils;



    /**
     * 分页搜索文章
     */
    @GetMapping("/search")
    public R<?> essaySearch(Integer current, Integer size,String kind) {
        return R.success(essayService.essaySearch(current,size,kind));
    }


    /**
     * 分页查询所有数据
     */
    @GetMapping("/paginatedQueries")
    public R<?> page(Integer current, Integer size, EssayDto essayDto) {
        return R.success(essayService.paginatedQueries(current, size, essayDto));
    }

    /**
     * 根据文章id查询文章
     */
    @GetMapping("/{id}")
    public R<?> getById(@PathVariable("id") Long id) {
        return R.success(essayService.getById(id));
    }

    /**
     * 查询热点文章
     */
    @GetMapping("/hotArticle")
    public R<?> getHotArticle(String[] kind) {
        return R.success(essayService.getHotArticle(kind));
    }

    /**
     * 文章点赞的修改和减少操作
     *
     *
     */
    //赞增加
    @PutMapping("/support")
    public R<?> support(@RequestHeader("AUTHORIZATION_TOKEN") String token, SupportDto supportDto) {
        UpdateWrapper<Essay> wrapper = new UpdateWrapper<>();
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return R.error("登录过期！");
        }
        boolean aBoolean = false;
        //进行点赞，或取消点赞
        if (supportDto.getBool()) {
            Boolean bool = redisUtils.checkSetValue(RedisUtils.SUPPORT, supportDto.getWid(), String.valueOf(user.getId()));
            //是否已经点赞过
            if(!bool){
                redisUtils.addSetValue(RedisUtils.SUPPORT, supportDto.getWid(), String.valueOf(user.getId()));
                wrapper.eq("id", supportDto.getWid()).setSql("support = support + 1");
                aBoolean =  essayService.update(null, wrapper);
            }
        } else {
            redisUtils.delSetValue(RedisUtils.SUPPORT, supportDto.getWid(), String.valueOf(user.getId()));
            wrapper.eq("id", supportDto.getWid()).setSql("support = support - 1");
            aBoolean = essayService.update(null, wrapper);
        }
        return R.success(aBoolean ? "成功":"已经点赞过了",null);
    }

    /**
     * 检查文章是否已经点赞
     *
     *
     */
    @GetMapping("/supportStatue/{id}")
    public R<?> supportStatue(@RequestHeader("AUTHORIZATION_TOKEN") String token, @PathVariable("id") Long id) {
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return R.error("登录过期！");
        }
        return R.success(redisUtils.checkSetValue(RedisUtils.SUPPORT, id, String.valueOf(user.getId())));
    }

    /**
     * 文章浏览增加
     *
     *
     */
    @GetMapping("/visit")
    public R<?> supportStatue( Long id) {
        UpdateWrapper<Essay>  wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).setSql("visit = visit + 1");
        return R.success(essayService.update(null,wrapper));
    }


}

